package com.ruoyi.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.blog.domain.BlogComment;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.blog.domain.bo.BlogCategoryBo;
import com.ruoyi.blog.domain.vo.BlogCategoryVo;
import com.ruoyi.blog.domain.BlogCategory;
import com.ruoyi.blog.mapper.BlogCategoryMapper;
import com.ruoyi.blog.service.IBlogCategoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 博客分类Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-02
 */
@RequiredArgsConstructor
@Service
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory> implements IBlogCategoryService {

    private final BlogCategoryMapper baseMapper;

    /**
     * 查询博客分类
     */
    @Override
    public BlogCategoryVo queryById(Long categoryId){
        return baseMapper.selectVoById(categoryId);
    }


    /**
     * 查询博客分类列表
     */
    @Override
    public List<BlogCategoryVo> queryList(BlogCategoryBo bo) {
        LambdaQueryWrapper<BlogCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogCategory> buildQueryWrapper(BlogCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), BlogCategory::getCategoryName, bo.getCategoryName());
        lqw.eq(StringUtils.isNotBlank(bo.getCategoryInfo()), BlogCategory::getCategoryInfo, bo.getCategoryInfo());
        lqw.eq(bo.getParentId() != null, BlogCategory::getParentId, bo.getParentId());
        lqw.eq(bo.getIsShow() != null, BlogCategory::getIsShow, bo.getIsShow());
        lqw.orderBy(bo.getOrderBy() != null, bo.getOrderBy() != null && bo.getOrderBy() == 0,
            BlogCategory::getOrderBy, BlogCategory::getCreateTime);
        return lqw;
    }

    /**
     * 新增博客分类
     */
    @Override
    public Boolean insertByBo(BlogCategoryBo bo) {
        BlogCategory add = BeanUtil.toBean(bo, BlogCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCategoryId(add.getCategoryId());
        }
        return flag;
    }

    /**
     * 修改博客分类
     */
    @Override
    public Boolean updateByBo(BlogCategoryBo bo) {
        BlogCategory update = BeanUtil.toBean(bo, BlogCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除博客分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<Long> getAllIdByParent(Long categoryId) {
        return baseMapper.getAllIdByParent(categoryId);
    }
}
